package tech.aistar.controller;

import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.aistar.pojo.Order;
import tech.aistar.pojo.User;
import tech.aistar.pojo.UserInfo;
import tech.aistar.pojo.result.ResultReturn;
import tech.aistar.pojo.vo.UserInfoVo;
import tech.aistar.service.OrderService;
import tech.aistar.service.UserInfoService;

import java.util.UUID;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 分页查询订单
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
        @GetMapping("/list/{pageNum}/{pageSize}")
    public ResultReturn list(HttpServletRequest request, @PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        Integer userId = (Integer)request.getAttribute("userId");
        log.info("订单分页查询：{}",userId);
        log.info("pageNum:{}",pageNum);
        log.info("pageSize:{}",pageSize);
        return orderService.pageQuery(userId,pageNum,pageSize);
    }

    /**
     * 生成订单
     * @param request
     * @param order
     * @return
     */
    @PostMapping
    public ResultReturn add(HttpServletRequest request, @RequestBody Order order) {
        Integer userId = (Integer)request.getAttribute("userId");
        log.info("订单生成：{}",order);
        order.setUser_id(userId);
        order.setStatus(0);
        order.setOrder_id(UUID.randomUUID().toString());
        return orderService.initOrder(order);
    }

    /**
     * 删除订单
     * @param request
     * @param order_id
     * @return
     */
    @DeleteMapping("/{order_id}")
    public ResultReturn delete(HttpServletRequest request,@PathVariable String order_id) {
        log.info("删除订单：{}",order_id);
        return orderService.delOrder(order_id);
    }

    /**
     * update order
     * @param request
     * @param order
     * @return
     */
    @PutMapping("/update")
    public ResultReturn update(HttpServletRequest request, @RequestBody Order order) {
        log.info("修改订单：{}",order);
        return orderService.updateOrder(order);
    }
}
